package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User selectUserByEmailAndPassword(String email, String password) {
        return userMapper.selectUserByEmailAndPassword(email, password);
    }

    @Override
    public void save(User user) {
        userMapper.insert(user);
    }

    @Override
    public void register(User user) {
        User u = userMapper.selectUserByEmailAndPassword(user.getEmail(), null);
        if (u != null) {
            throw new RuntimeException("邮箱已经被注册");
        }
        // 邮箱不存在,可以注册
        // 设置默认的头像和背景墙
        user.setHeadImgUrl("/img/user/head.jpg");
        user.setCoverImgUrl("img/user/bg.jpeg");
        userMapper.insert(user);
    }

    @Override
    public User login(String email, String password) {
        User user = userMapper.selectUserByEmailAndPassword(email, password);
        // 用户不存在
        if (user == null) {
            throw new RuntimeException("邮箱或者密码不正确");
        }
        return user;
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void updateInfo(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public User get(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void insertRelationWithTravelId(Long userId, Long travelId,Long status) {
        userMapper.insertRelationWithTravelId(userId,travelId,status);
    }

    @Override
    public void insertRelationWithStrategyId(Long userId, Long strategyId,Long status) {
        userMapper.insertRelationWithStrategyId(userId,strategyId,status);
    }


    @Override
    public void insertRelationWithDailyId(Long userId, Long dailyId, Long status) {
        userMapper.insertRelationWithDailyId(userId,dailyId,status);
    }

    @Override
    public void deleteRelationWithDailyId(Long userId, Long dailyId) {
        userMapper.deleteRelationWithDailyId(userId,dailyId);
    }

    @Override
    public void deleteRelationWithTravelId(Long userId, Long travelId) {
        userMapper.deleteRelationWithTravelId(userId,travelId);
    }

    @Override
    public void deleteRelationWithStrategyId(Long userId, Long strategyId) {
        userMapper.deleteRelationWithStrategyId(userId,strategyId);
    }

    @Override
    public User checkHead(String email) {
        return userMapper.checkHead(email);
    }


}
